package com.ibm.simpleserver;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

public class BasicRunnable implements Runnable {
	private static int visitorNumber = 0;

	private String greeting = "Welcome to the SimpleServer.";
	private String response = "You are visitor number: ";
	private final int myVisitorNumber;
	private Socket connection;

	public BasicRunnable(Socket s) {
		connection = s;

		synchronized (BasicRunnable.class) {
			visitorNumber++;
			myVisitorNumber = visitorNumber;
		}
	}

	public void run() {
		try {
			OutputStream outStream = connection.getOutputStream();
			OutputStreamWriter output = new OutputStreamWriter(outStream);
			PrintWriter writer = new PrintWriter(output);
			writer.println(greeting);
			writer.print(response);
			writer.print(myVisitorNumber);
			writer.print("\n");
			writer.flush();
			writer.close();
		} catch (Exception e) {
			System.out.println("Something weird happened: " + e);
			e.printStackTrace(System.out);
		}
	}

}
